(function() {
  var $page;

<% if @old_page_layout != @page.page_layout -%>
  Alchemy.ElementsWindow.reload();
  Alchemy.growl('<%= j Alchemy.t(:page_layout_changed_notice) %>');
<% end -%>

<% if @while_page_edit -%>

  Alchemy.reloadPreview();
  $('#page_<%= @page.id %>_status').replaceWith('<%= j render("page_status", page: @page) %>');

<% else -%>

  var page_html = "<%= j render('page', page: @page) %>";
  var compiler = Handlebars.compile(page_html);
  var tree = JSON.parse('<%== @tree.to_json %>');
  var html = compiler(tree.pages[0]);
  $('#page_<%= @page.id %>').replaceWith(html);
  $page = $('#page_<%= @page.id %>');

  <% if @page.locked? && @page.locker == current_alchemy_user -%>
    $('#locked_page_<%= @page.id %> > a').html('<%= @page.name %>');
  <% end -%>

  <% if @page.restricted? -%>
    $('.page_status:nth-child(3)', $page).addClass('restricted', 'not_restricted').removeClass('not_restricted');
  <% elsif @page.redirects_to_external? -%>
    $('span.redirect_url', $page).html('&raquo; <%= Alchemy.t("Redirects to") %>: <%= h @page.external_urlname %>');
  <% else -%>
    $('.page_status:nth-child(3)', $page).addClass('not_restricted').removeClass('restricted');
  <% end -%>

<% end -%>

  Alchemy.growl("<%= j @notice %>");
  Alchemy.closeCurrentDialog();
})()
